package team.seekers.hula.course.resource.problemSubmit.entity.dto;

import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import team.seekers.hula.common.entity.BaseDTO;
import team.seekers.hula.common.entity.enums.ProblemTypeEnum;

import java.util.Date;
import java.util.List;

/**
 * @author GH
 * @date 2022/5/2 16:03
 **/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class SubmissionDTO extends BaseDTO {

    /**
     * 主键, 分布式id
     */
    private String id;
    /**
     * 题目id
     */
    private String pId;
    /**
     * 提交者id
     */
    private String uId;
    /**
     * 关联的视频/文档id
     */
    private String trId;
    /**
     * 关联的单元测试id
     */
    private String exId;
    /**
     * zipFile主键
     */
    private Long fId;
    /**
     * 提交端ipv4
     */
    private String ipv4;
    /**
     * 提交结果
     */
    private String result;
    /**
     * 评测分数
     */
    private Float score;
    /**
     * 第几次提交
     */
    private Integer count;
    /**
     * 有效提交, 0无效, 1有效
     */
    private Integer valid;
    /**
     * 是否公开, 0不公开, 1公开
     */
    private Integer isPublic;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private Date gmtCreate;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private Date gmtModified;

    public static Float judge(String rightAnswer, Integer criteria, String answer, Integer type) {
        float res = 0;
        ProblemTypeEnum typeEnum = ProblemTypeEnum.getByCode(type);
        if (typeEnum != null) {
            switch (typeEnum) {
                case CHOICE:
                    int ansNum = Integer.parseInt(rightAnswer);
                    int resNum = Integer.parseInt(answer);
                    if ((ansNum & resNum) == ansNum) {
                        res = criteria;
                    }
                    break;
                case CHOICE_PLUS:
                    ansNum = Integer.parseInt(rightAnswer);
                    resNum = Integer.parseInt(answer);
                    if ((ansNum & resNum) == ansNum) {
                        res = criteria;
                    }
                    break;
                case FILL:
                    // 将rightAnswer拆分，可能会有多个答案
                    List<String> rightAnswers = JSONArray.parseArray(rightAnswer, String.class);
                    if(rightAnswers.contains(answer))
                        res = criteria;
                    break;
                default:
                    break;
            }
        }
        return res;
    }

    private static int getCount(int num) {
        int count = 0;
        while (num != 0) {
            num = num & (num - 1);
            count++;
        }
        return count;
    }
}
